<template>
  <div>
    <div class="diva">
      <h1>商品管理</h1>
      <el-button style="height: 40px; margin-top: 15px" @click="add"
        >商品添加</el-button
      >
    </div>
    <!-- 搜索 -->
    <el-select @change="a" v-model="select" slot="prepend" placeholder="请选择">
      <el-option label="按商品id查询" value="1"></el-option>
      <el-option label="按商品名字查询" value="2"></el-option>
    </el-select>
    <el-input
      style="width: 200px; margin-left: 10px"
      placeholder="请输入内容"
      clearable
      v-model="searchname"
    >
    </el-input>
    <el-button @click="search">搜索</el-button>
    <!-- 表格 -->
    <template>
      <el-table :data="goods" style="width: 100%; margin-top: 10px">
        <el-table-column prop="id" label="id" width="180"> </el-table-column>
        <el-table-column prop="name" label="信息" width="180">
        </el-table-column>
        <el-table-column prop="price" label="价格"> </el-table-column>
        <el-table-column prop="price" label="状态">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1">
              已上架
              <el-button type="danger" @click="gai(scope.row)">下架</el-button>
            </div>
            <div v-if="scope.row.status == 2">
              已下架
              <el-button type="danger" @click="gai(scope.row)">上架</el-button>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="操作">
          <template slot-scope="scope">
            <el-button @click="cha(scope.row.id)">查看</el-button>
            <el-button @click="chaa(scope.row.id)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <fenyeqi
      :total="fenye.total"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
  </div>
</template>

<script>
import { goodlist, gai, search, searchid } from "../../../http/http";
import fenyeqi from "../../../components/fenyeqi";
export default {
  name: "",
  components: {
    fenyeqi,
  },
  props: [],
  data() {
    return {
      goods: [],
      // 分页
      fenye: {
        pagesize: 3,
        pagenum: 1,
        total: 1,
      },
      // 搜索
      select: "",
      searchname: "",
      i: 0,
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.goodlist();
  },
  methods: {
    // 获取数据
    async goodlist() {
      const { data: res } = await goodlist(this.fenye);
      this.goods = res.data.list;
      this.fenye.total = res.data.total;
    },
    // 产品上下架
    async gai(status) {
      let s = 1;
      if (status.status == 1) {
        s = 2;
      } else {
        s = 1;
      }
      const { data: res } = await gai(
        `/api/manage/product/set_sale_status.do?productId=${status.id}&status=${s}`
      );
      this.goodlist();
      this.$message.success(res.data);
    },
    handleSizeChange(size) {
      console.log(size);
      this.fenye.pagesize = size;
      this.goodlist();
    },
    handleCurrentChange(page) {
      console.log(page);
      this.fenye.pagenum = page;
      this.goodlist();
    },
    // 商品搜索
    a(i) {
      this.i = i;
    },
    async search() {
      console.log(this.i);
      if (this.i == 0) {
        this.$message.error("请选择id搜索或者名称搜索");
        return false;
      }
      if (this.i == 1) {
        const { data: res } = await searchid(this.searchname);
        this.goods = res.data.list;
        this.fenye.total = res.data.total;
        this.$message.success("搜索成功");
      } else {
        const { data: res } = await search(this.searchname);
        this.goods = res.data.list;
        this.fenye.total = res.data.total;
        this.$message.success("搜索成功");
      }
    },
    // 查看详情
    cha(id) {
      this.$router.push({
        name: "details",
        params: {
          id: id,
        },
      });
    },
    chaa(id) {
      this.$router.push({
        name: "edit",
        params: {
          id: id,
        },
      });
    },
    add() {
      this.$router.push("/addshop");
    },
  },
};
</script>

<style lang='scss' scoped>
.diva {
  display: flex;
  justify-content: space-between;
}
</style>
